package com.lianniu.mgr.cms.service.impl;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.annotation.Resource;

import org.apache.log4j.Logger;
import org.springframework.stereotype.Service;

import com.alibaba.fastjson.JSON;
import com.lianniu.mgr.base.BaseResult;
import com.lianniu.mgr.cms.dao.IHelpCenterInfoDao;
import com.lianniu.mgr.cms.model.HelpCenterInfoVo;
import com.lianniu.mgr.cms.service.IHelpCenterInfoService;
import com.lianniu.mgr.constants.ErrorCode;
import com.lianniu.mgr.pagination.PaginationInfo;
import com.lianniu.mgr.util.StringUtil;

@Service
public class HelpCenterInfoServiceImpl implements IHelpCenterInfoService {

	private final static Logger logger = Logger.getLogger(HelpCenterInfoServiceImpl.class);
	  
	@Resource
	IHelpCenterInfoDao helpCenterInfoDao;
	
	@Override
	public BaseResult insertHelpCenterInfo(HelpCenterInfoVo curHelpCenterInfoVo) {
		// TODO Auto-generated method stub
	    logger.info("insertHelpCenterInfo begin");
	    logger.info("insertHelpCenterInfo: input data check begin");
	    logger.info("insertHelpCenterInfo curHelpCenterInfoVo:" +JSON.toJSONString(curHelpCenterInfoVo));
	   
	    
	    BaseResult baseResult;
	    if (StringUtil.isEmpty(curHelpCenterInfoVo.getTitle())) {
	      baseResult = BaseResult.fail(ErrorCode.ParaCheckErrorRetCode, ErrorCode.ParaCheckErrorRetInfo);
	      return baseResult;
	    }	     
	    if (StringUtil.isEmpty(curHelpCenterInfoVo.getParentId())) {
	    	curHelpCenterInfoVo.setParentId("0");
		  }  
		    
	    if (StringUtil.isEmpty(curHelpCenterInfoVo.getSort())) {
	    	curHelpCenterInfoVo.setSort("0");
		  }   
	    
	    logger.info("insertHelpCenterInfo: input data check end");	    
	    return BaseResult.success(helpCenterInfoDao.insertHelpCenterInfo(curHelpCenterInfoVo));
	}

	@Override
	public BaseResult deleteById(String Id) {
	   // TODO Auto-generated method stub
	   logger.info("deleteById begin");
	   logger.info("deleteById Id:" +Id);
				  
	   return BaseResult.success(helpCenterInfoDao.deleteById(Id));
	}

	@Override
	public BaseResult deleteHelpCenterInfoBatch(String... id) {
		// TODO Auto-generated method stub
	    logger.info("deleteHelpCenterInfoBatch begin");
	    logger.info("deleteHelpCenterInfoBatch Ids:" +id.toString());
	    logger.info("deleteHelpCenterInfoBatch: input data check begin");
	   
	    BaseResult baseResult;
	    if (StringUtil.isEmpty(id.toString())) {
	      baseResult =  BaseResult.fail(ErrorCode.ParaCheckErrorRetCode, ErrorCode.ParaCheckErrorRetInfo);
	      return baseResult;
	    }
	    logger.info("deleteHelpCenterInfoBatch: input data check end");
	    
	    return BaseResult.success(helpCenterInfoDao.deleteHelpCenterInfoBatch(id));
	}

	@Override
	public BaseResult updateById(HelpCenterInfoVo curHelpCenterInfoVo) {
		// TODO Auto-generated method stub
	    logger.info("updateById begin");
	    logger.info("updateById: update data check begin");
	    logger.info("updateById curHelpCenterInfoVo:" +JSON.toJSONString(curHelpCenterInfoVo));
	  
	    
	    BaseResult baseResult;
	    if (StringUtil.isEmpty(curHelpCenterInfoVo.getTitle())) {
	      baseResult =BaseResult.fail(ErrorCode.ParaCheckErrorRetCode, ErrorCode.ParaCheckErrorRetInfo);
	      return baseResult;
	    }
	   
	    if (StringUtil.isEmpty(curHelpCenterInfoVo.getSort())) {
	    	curHelpCenterInfoVo.setSort("0");
		}  
	    
	    logger.info("updateById: update data check end");
	    
	    return BaseResult.success(helpCenterInfoDao.updateById(curHelpCenterInfoVo));
	}

	@Override
	public BaseResult selectByHelpCenterInfoForPage(HelpCenterInfoVo curHelpCenterInfoVo, PaginationInfo paginationInfo) {
		// TODO Auto-generated method stub
	    logger.info("selectByHelpCenterInfoForPage begin.");
	    if (!StringUtil.isEmpty(curHelpCenterInfoVo.getTitle())) {
	    	curHelpCenterInfoVo.setTitle(curHelpCenterInfoVo.getTitle().replace(" ", ""));
	    }
	    logger.info("selectByHelpCenterInfoForPage Title:" +curHelpCenterInfoVo.getTitle());
	
	    logger.info("selectByHelpCenterInfoForPage paginationInfo:" +JSON.toJSONString(paginationInfo));
	    
	    curHelpCenterInfoVo.setPaginationInfo(paginationInfo);
	    List<HelpCenterInfoVo> list;
	    if(!StringUtil.isEmpty(curHelpCenterInfoVo.getParentId())){
	      list=helpCenterInfoDao.selectByHelpCenterInfoForPage(curHelpCenterInfoVo);
	    }
	    else{
	    	list=helpCenterInfoDao.selectByHelpCenterParentInfoForPage(curHelpCenterInfoVo);
	    }
	    Map<String,Object> retmap=new HashMap<String,Object>();
	    retmap.put("total", paginationInfo.getTotalRecord());
	    retmap.put("curPage", paginationInfo.getCurPage());
	    retmap.put("rows", list);
	    logger.info("selectByHelpCenterInfoForPage:  end");    
	    
	    return BaseResult.success(retmap); 
	}

	@Override
	public BaseResult selectById(HelpCenterInfoVo curHelpCenterInfoVo) {
		// TODO Auto-generated method stub
	    logger.info("selectById begin");
	    logger.info("selectById Id:" +curHelpCenterInfoVo.getId());
	  
	    return BaseResult.success(helpCenterInfoDao.selectById(curHelpCenterInfoVo.getId()));
	}

	@Override
	public Boolean checkTitle(String title,String parentId) {
		// TODO Auto-generated method stub
	    logger.info("checkTitle begin");
	    logger.info("checkTitle title:" +title);
	    logger.info("checkTitle parentId:" +parentId);
	    logger.info("checkTitle: input data check begin");   
	  
	    if (StringUtil.isEmpty(title)) {       
	      return false;
	    }
	    if (StringUtil.isEmpty(parentId)) {       
		      return false;
		    }
	    logger.info("checkTitle: input data check end");
	    
	    HelpCenterInfoVo helpCenterInfoVo=new HelpCenterInfoVo();	   
	    helpCenterInfoVo.setTitle(title);
	    helpCenterInfoVo.setParentId(parentId);
	    int count=helpCenterInfoDao.count(helpCenterInfoVo);
	    if(count>0){
	      return false;      
	    }
	    else{
	      return true;
	    }   
	}

}
